package util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;


public class DateUtil {
	private static final Locale LOCALE_DEFAULT = new Locale("en", "EN"); 
    public static Timestamp getTimestampFromDDMMYYYYHHMMSS(String DDMMYYYY, int diffYear)
    {
      if (DDMMYYYY != null)
      {
        int ddInt = Integer.parseInt(DDMMYYYY.substring(0, 2));
        int mmInt = Integer.parseInt(DDMMYYYY.substring(3, 5));
        int yyyyInt = Integer.parseInt(DDMMYYYY.substring(6, 10));
        int hh24Int = 0;
        int miInt = 0;
        int ssInt = 0;

        try
        {
          String time[] = DDMMYYYY.split(":");
          hh24Int = Integer.parseInt(time[1]);
          miInt = Integer.parseInt(time[2]);
          ssInt = Integer.parseInt(time[3]);
        }
        catch (Exception e)
        {
          //no operation
        }

        Calendar calendar = new GregorianCalendar(yyyyInt + diffYear,
                                                  mmInt - 1, ddInt, hh24Int, miInt, ssInt);

        return getTimestampFromCalendar(calendar);
      }
      else
      {
        return getCurrentTimestamp();
      }
    }
  
  public static Timestamp getCurrentTimestamp()
  {
    return getTimestampFromDate(null);
  }

  public static Timestamp getTimestampFromDate(java.util.Date date)
  {
    if (date != null)
    {
      return new Timestamp(date.getTime());
    }
    else
    {
      return new Timestamp(System.currentTimeMillis());
    }
  }

  public static String getCurrentDDMMYYYY_CE()
  {
    String tsStr = getCurrentTimestamp().toString();
    tsStr = tsStr.substring(8, 10) + "/" + tsStr.substring(5, 7) + "/" + tsStr.substring(0, 4);

    return tsStr;
  }

  public static String getCurrentYYYY_CE(){
    String tsStr = getCurrentTimestamp().toString();
    tsStr = tsStr.substring(0,4);
    return tsStr;
  }

  public static String getCurrentDDMMYYYYhh24miss_CE()
  {
    String tsStr = getCurrentTimestamp().toString();
    tsStr = tsStr.substring(8, 10) + "/" + tsStr.substring(5, 7) + "/" +
        tsStr.substring(0, 4) + ":" + tsStr.substring(11, 19);

    return tsStr;
  }

//  public static String getStepDDMMYYYY(String DDMMYYYY, int difDay)
//  {
//    Timestamp ts =  getTimestampFromDDMMYYYY(DDMMYYYY, 0);
//    long cTime = ts.getTime();
//    long oneday = 86400000;
//    String difDayStr = "" + difDay;
//    if(difDayStr.startsWith("-"))
//    {
//      difDayStr = difDayStr.substring(1);
//    }
//    long difTime = oneday * Integer.parseInt(difDayStr);
//    if(difDay > 0)
//    {
//      cTime = cTime + difTime;
//    }
//    else
//    {
//      cTime = cTime - difTime;
//    }
//    java.util.Date date = new java.util.Date(cTime);
//    return getDDMMYYYYFromDate(date, 0);
//  }

  public static String getCurrentDDMMYYYY_BU()
  {
    return getCurrentDDMMYYYY_CE().substring(0, 6) + (Integer.parseInt(getCurrentDDMMYYYY_CE().substring(6, 10)) + 543);
  }

  public static String getCurrentDDMMYYYYhh24miss_BU()
  {
    String tsStr = getCurrentTimestamp().toString();
    tsStr = tsStr.substring(8, 10) + "/" + tsStr.substring(5, 7) + "/" +
        (Integer.parseInt(tsStr.substring(0, 4)) + 543) + ":" + tsStr.substring(11, 19);

    return tsStr;
  }

//  public static Timestamp getTimestampFromTimestamString(String tsStr)
//  {
//    if (tsStr != null)
//    {
//      String DDMMYYYY = getDDMMYYYYhh24missFromTimestampString(tsStr, 0);
//
//      return getTimestampFromDDMMYYYY(DDMMYYYY, 0);
//    }
//    else
//    {
//      return getCurrentTimestamp();
//    }
//  }

  //DDMMYYYY = 31/12/2004
  //DDMMYYYY = 31/12/2004:13:30:59
  public static Timestamp getTimestampFromDDMMYYYY(String DDMMYYYY, int diffYear)
  {
    if (DDMMYYYY != null)
    {
      int ddInt = Integer.parseInt(DDMMYYYY.substring(0, 2));
      int mmInt = Integer.parseInt(DDMMYYYY.substring(3, 5));
      int yyyyInt = Integer.parseInt(DDMMYYYY.substring(6, 10));
      int hh24Int = 0;
      int miInt = 0;
      int ssInt = 0;

      try
      {
        hh24Int = Integer.parseInt(DDMMYYYY.substring(11, 13));
        miInt = Integer.parseInt(DDMMYYYY.substring(14, 16));
        ssInt = Integer.parseInt(DDMMYYYY.substring(17, 19));
      }
      catch (Exception e)
      {
        //no operation
      }

      Calendar calendar = new GregorianCalendar(yyyyInt + diffYear,
                                                mmInt - 1, ddInt, hh24Int, miInt, ssInt);

      return getTimestampFromCalendar(calendar);
    }
    else
    {
      return getCurrentTimestamp();
    }
  }

  public static Timestamp getTimestampFromCalendar(Calendar calendar)
  {
    Timestamp timestamp = null;

    if (calendar != null)
    {
      java.util.Date date = calendar.getTime();
      timestamp = new Timestamp(date.getTime());
    }
    else
    {
      timestamp = getCurrentTimestamp();
    }

    return timestamp;
  }

  public static String getDDMMYYYYFromDate(java.util.Date date, int diffYear)
  {
    if (date != null)
    {
      return getDDMMYYYYFromTimestampString(getTimestampFromDate(date).toString(), diffYear);
    }
    else
    {
      return getCurrentDDMMYYYY_CE();
    }
  }

  public static String getDDMMYYYYFromTimestamp(Timestamp timestamp, int diffYear)
  {
    String tsStr = null;

    if (timestamp != null)
    {
      tsStr = timestamp.toString();
    }
    else
    {
      tsStr = getCurrentTimestamp().toString();
    }

    return getDDMMYYYYFromTimestampString(tsStr, diffYear);
  }

  public static String getDDMMYYYYFromTimestampString(String tsStr,
      int diffYear)
  {
    if (tsStr != null)
    {
      ;
    }
    else
    {
      tsStr = getCurrentTimestamp().toString();
    }

    tsStr = tsStr.substring(8, 10) + "/" + tsStr.substring(5, 7) + "/" + (Integer.parseInt(tsStr.substring(0, 4)) + diffYear);

    return tsStr;
  }

//  public static String getDDMMYYYYhh24missFromTimestamp(Timestamp timestamp,
//      int diffYear)
//  {
//    String tsStr = null;
//
//    if (timestamp != null)
//    {
//      tsStr = timestamp.toString();
//    }
//    else
//    {
//      tsStr = getCurrentTimestamp().toString();
//    }
//
//    return getDDMMYYYYFromTimestampString(tsStr, diffYear);
//  }

  public static String getDDMMYYYYhh24missFromTimestampString(String tsStr, int diffYear)
  {
    if (tsStr != null)
    {
      ;
    }
    else
    {
      tsStr = getCurrentTimestamp().toString();
    }

      tsStr = tsStr.substring(0, 2) + "/" + tsStr.substring(3, 5) + "/" + (Integer.parseInt(tsStr.substring(6, 10)) + diffYear) + ":" + tsStr.substring(11, 19);

    return tsStr;
  }
  
  public static String getDDMMYYYYhh24missFromDate(java.util.Date date, boolean isShowSecond, String defaultValue){
      if(date == null) return defaultValue == null? "" : defaultValue;
      String datePattern = isShowSecond? "dd/MM/yyyy:HH:mm:ss" : "dd/MM/yyyy:HH:mm";
      SimpleDateFormat sp = new SimpleDateFormat(datePattern, LOCALE_DEFAULT);
      return sp.format(date);
  }
  
  public static String getDDMMYYYYFromDate(java.util.Date date, String defaultValue){
      if(date == null) return defaultValue == null? "" : defaultValue;
      String datePattern = "dd/MM/yyyy";
      SimpleDateFormat sp = new SimpleDateFormat(datePattern, LOCALE_DEFAULT);
      return sp.format(date);
  }
  
  public static String getHH24missFromDate(java.util.Date date, boolean isShowSecond, String defaultValue){
      if(date == null) return defaultValue == null? "" : defaultValue;
      String datePattern = isShowSecond? "HH:mm:ss" : "HH:mm";
      SimpleDateFormat sp = new SimpleDateFormat(datePattern, LOCALE_DEFAULT);
      return sp.format(date);  
  }

  public static int getDiffDays(String DDMMYYYY1, String DDMMYYYY2)
  {
      //86400000 = 1 day
  long oneday = 86400000;
  long day2day = getTimestampFromDDMMYYYY(DDMMYYYY2, 0).getTime() -
      getTimestampFromDDMMYYYY(DDMMYYYY1, 0).getTime();
  return Integer.parseInt(String.valueOf(day2day / oneday));

  }

  public static String getCurrentYearBU()
  {
    return getCurrentDDMMYYYY_BU().substring(6);
  }

  public static String getCurrentYearCE()
  {
    return getCurrentDDMMYYYY_CE().substring(6);
  }

  public static String getDDMMYYYYFromYYYYMMDDString (String tsStr){
      return tsStr.substring(8,10) + "/" + tsStr.substring(5,7) + "/" + tsStr.substring(0,4);
  }
  
  public static String getTimeFromYYYYMMDDString (String tsStr){
     return tsStr.substring(11,16);
  }

  public static String getYYYYMMDDFromTimestampString(String tsStr, int diffYear, String textSeparate)
  {
    if (tsStr != null)
    {
      ;
    }
    else
    {
      tsStr = getCurrentTimestamp().toString();
    }

    tsStr = (Integer.parseInt(tsStr.substring(0, 4)) + diffYear + textSeparate + tsStr.substring(5,
        7) + textSeparate + tsStr.substring(8, 10));

    return tsStr;
  }
  
    //23:59
    public static String getCurrentClockNoSec()
    {
    return getTimestampFromDate(null).toString().substring(11, 16);
    }

  //23:59:59
  public static String getCurrentClock()
  {
    return getTimestampFromDate(null).toString().substring(11, 19);
  }

  //23:59:59
  public static String getClockFromDate(java.util.Date date)
  {
    if (date == null)
    {
      return getCurrentClock();
    }
    else
    {
      return date.toString().substring(11, 19);
    }
  }

  /**
   * get dd mm yyyy ex. 20/12/2005
   * @param dateStr String
   * @param separate String -
   * @return String
   */
  public static String getMMDDYYYYFromDDMMYYYY(String dateStr, String separate)
  {
    String dateturn = null;
    try
    {
      dateturn = dateStr.substring(3, 5) + separate + dateStr.substring(0, 2) + separate + dateStr.subSequence(6, 10);

    }
    catch (Exception e)
    {
      System.out.println(e.getMessage());
    }
    return dateturn;
  }

  /**
   * get dd mm yyyy ex. 20/12/2005
   * and use default separate -
   * @param dateStr String
   * @return String
   */
  public static String getMMDDYYYYFromDDMMYYYY(String dateStr)
  {
    return getMMDDYYYYFromDDMMYYYY(dateStr, "-");
  }
  /**
   *
   * @return String
   */
//  public static String getFirstDateOfCurrentMonth()
//  {
//    String[] fM = getCurrentDDMMYYYY_CE().split("/");
//    return "01/" + fM[1] + "/" + fM[2];
//
//  }

  public static java.util.Date getDateNoTime(String dateStr) throws ParseException
  {
    String pattern = "dd/MM/yyyy";
    if(dateStr == null){
      dateStr = getCurrentDDMMYYYY_CE();
    }
    DateFormat formatter = new SimpleDateFormat(pattern);
    java.util.Date date = (java.util.Date)formatter.parse(dateStr);
    return date;
  }


//    public static Date getSqlDateNoTime(String dateStr) throws ParseException
//    {
//      Date date = null;
//      String pattern = "yyyy-MM-dd";
//      if(!dateStr.equals("")){
//          DateFormat formatter = new SimpleDateFormat(pattern);
//          date = new Date ((formatter.parse(dateStr.substring(0,10))).getTime());
//      }
//      return date;
//    }

//  public static java.util.Date getDateFromString(String dateStr) throws ParseException
//  {
//    String pattern = null;
//    if(dateStr == null){
//      pattern = "dd/MM/yyyy:HH:mm:ss";
//      dateStr = getCurrentDDMMYYYYhh24miss_CE();
//    }else if(dateStr.length() == 19){
//      pattern = "dd/MM/yyyy:HH:mm:ss";
//    }else if(dateStr.length() == 10){
//      pattern = "dd/MM/yyyy";
//    }
//    DateFormat formatter = new SimpleDateFormat(pattern, new Locale("th", "TH"));
//    java.util.Date date = (java.util.Date)formatter.parse(dateStr);
//    return date;
//  }
  
  public static String getDDMMYYYYHHMMFromTimestamp(Timestamp timestamp){
    String tsStr = null;
    if (timestamp != null){
        tsStr = timestamp.toString();
        try{
            tsStr = tsStr.substring(8,10) + "/" + tsStr.substring(5,7) + "/" + tsStr.substring(0,4)+" "
            +tsStr.substring(11,13) + ":" + tsStr.substring(14,16);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
    return tsStr;
  }
  public static String getDDMMYYYYFromTimestamp(Timestamp timestamp){
    String tsStr = null;
    if (timestamp != null){
        tsStr = timestamp.toString();
        try{
            tsStr = tsStr.substring(8,10) + "/" + tsStr.substring(5,7) + "/" + tsStr.substring(0,4);
        }catch (Exception e){
        	System.out.println(e.getMessage());
        }
    }
    return tsStr;
  }
  public static String getHHMMFromTimestamp(Timestamp timestamp){
    String tsStr = null;
    if (timestamp != null){
        tsStr = timestamp.toString();
        try{
            tsStr = tsStr.substring(11,13) + ":" + tsStr.substring(14,16);
        }catch (Exception e){
        	System.out.println(e.getMessage());
        }
    }
    return tsStr;
  }
  
    public static String getHourMinSecFromTimestamp(Timestamp timestamp)
    {
      String tsStr = null;

      if (timestamp != null)
      {
        tsStr = timestamp.toString();
      }
      else
      {
        tsStr = null;
      }

      return getHourMinSecFromTimestampString(tsStr);
    }

    public static String getHourMinSecFromTimestampString(String tsStr)
    {
      if (tsStr != null)
      {
        ;
      }
      else
      {
        tsStr = getCurrentTimestamp().toString();
      }

      tsStr = tsStr.substring(11, 19);

      return tsStr;
    }
    
    public static String getHourMinFromTimestamp(Timestamp timestamp)
    {
      String tsStr = null;

      if (timestamp != null)
      {
        tsStr = timestamp.toString();
      }
      else
      {
        tsStr = null;
      }

      return getHourMinFromTimestampString(tsStr);
    }

    public static String getHourMinFromTimestampString(String tsStr)
    {
      if (tsStr != null)
      {
        ;
      }
      else
      {
        tsStr = getCurrentTimestamp().toString();
      }

      tsStr = tsStr.substring(11, 16);

      return tsStr;
    }

}
